@echo off
chcp 936 >nul
echo ====================================
echo    Microservices Launcher
echo ====================================
echo.

:: Check Java
java -version >nul 2>&1
if errorlevel 1 (
    echo ERROR: Java not found
    pause
    exit /b 1
)

:: Check Maven
mvn -version >nul 2>&1
if errorlevel 1 (
    echo ERROR: Maven not found
    pause
    exit /b 1
)

echo Environments OK
echo.
echo Starting compilation...
echo.

set ROOT=F:\shopping

echo [1/6] Compiling common-module...
cd /d "%ROOT%\common-module"
mvn clean install -DskipTests >nul
if errorlevel 1 (
    echo ERROR: common-module failed
    pause
    exit /b 1
)
echo Done

echo [2/6] Compiling auth-service...
cd /d "%ROOT%\auth-service"
mvn clean package -DskipTests >nul
if errorlevel 1 (
    echo ERROR: auth-service failed
    pause
    exit /b 1
)
echo Done

echo [3/6] Compiling user-service...
cd /d "%ROOT%\user-service"
mvn clean package -DskipTests >nul
if errorlevel 1 (
    echo ERROR: user-service failed
    pause
    exit /b 1
)
echo Done

echo [4/6] Compiling product-service...
cd /d "%ROOT%\product-service"
mvn clean package -DskipTests >nul
if errorlevel 1 (
    echo ERROR: product-service failed
    pause
    exit /b 1
)
echo Done

echo [5/6] Compiling order-service...
cd /d "%ROOT%\order-service"
mvn clean package -DskipTests >nul
if errorlevel 1 (
    echo ERROR: order-service failed
    pause
    exit /b 1
)
echo Done

echo [6/6] Compiling gateway-service...
cd /d "%ROOT%\gateway-service"
mvn clean package -DskipTests >nul
if errorlevel 1 (
    echo ERROR: gateway-service failed
    pause
    exit /b 1
)
echo Done

echo.
echo All compiled successfully!
echo.
echo Starting services...
echo.

echo Starting auth-service (port 8084)...
start "auth-service" cmd /k "cd /d "%ROOT%\auth-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting user-service (port 8081)...
start "user-service" cmd /k "cd /d "%ROOT%\user-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting product-service (port 8082)...
start "product-service" cmd /k "cd /d "%ROOT%\product-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting order-service (port 8083)...
start "order-service" cmd /k "cd /d "%ROOT%\order-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting gateway-service (port 8080)...
start "gateway-service" cmd /k "cd /d "%ROOT%\gateway-service" && java -jar target\*.jar"

echo.
echo ====================================
echo    Services started!
echo ====================================
echo.
echo Ports: Gateway=8080, Auth=8084, User=8081
echo       Product=8082, Order=8083
echo.
echo Nacos: http://192.168.152.48:8848/nacos
echo.
echo Press any key to exit...
pause >nul